package com.xxxx.server.config.security.exception;


import com.xxxx.server.pojo.RespBean;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobException {
    @ExceptionHandler(SQLException.class)
    public RespBean sqlexception(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            e.printStackTrace();
            return RespBean.error("有外键不可删除");
        }
        e.printStackTrace();
        return RespBean.error("数据库操作异常===>"+e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public RespBean exceptions(Exception e){
        if (e instanceof UsernameNotFoundException){
            e.printStackTrace();
            return RespBean.error(e.getMessage());
        }
        e.printStackTrace();
        return RespBean.error("异常===>"+e.getMessage());
    }
}
